namespace OX.Strongbind.Dynamic
{
    using Castle.DynamicProxy;
    using System;
    using System.Collections.Generic;

    internal class DynamicProxyGenerator : IProxyGenerator
    {
        public DynamicProxyGenerator()
        {
        }

        public T For<T>(Guid identity, IPropertyAccessNotifier notifier)
        {
            return (T)For(typeof(T), identity, notifier);
        }

        internal object For(Type instanceType, Guid identity, IPropertyAccessNotifier notifier)
        {
            DynamicProxyInterceptor interceptor = new DynamicProxyInterceptor(this, identity, notifier);
            ProxyGenerator generator = new ProxyGenerator();

            object instance;

            if(instanceType.IsInterface)
                instance = generator.CreateInterfaceProxyWithoutTarget(instanceType, interceptor);
            else
                instance = generator.CreateClassProxy(instanceType, interceptor);

            return instance;
        }
    }
}
